﻿Ext.define('app.prm.detail.Discuss', {
    extend: 'Ext.panel.Panel',
    border: true,
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.projectId = me.paramsId;
        me.items = [
            {
                border: false,
                region: 'north',
                split: false,
                bodyStyle: 'padding:3px',
                items: [
                    me.getTextTitle(),
                    me.getTextarea(),
                    me.getUploadPanel()
                ],
                bbar: [
                    "->",
                    { text: '提问', icon: '/Content/images/help.png', handler: function () { me.save(); } },
                    {
                        text: '重置', icon: '/Content/images/comment.png', handler: function () {
                            me.textReset();
                        }
                    }
                ]
            },
            me.getContent()];
        me.callParent();
    },
    textReset: function () {
        var me = this;
        me.getTextTitle().reset();
        me.getTextarea().reset();
        me.getUploadPanel().removeAllArry();
    },
    listeners: {
        afterrender: function (me) {
            me.initReplies(me);
        }
    },
    initReplies: function (me) {
        App.AjaxGet('/api/services/app/topic/GetTopic?projectId=' + me.projectId, function (data) {
            me.getContent().removeAll(true);
            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];

                var panel = Ext.create('app.prm.detail.DiscussDetail', {
                    topicId: record.id,
                    titleCollapse: true,
                    title: record.title + " (" + record.createBy + "." + record.createOn + ")",
                    htmlContent: record.content,
                    fileArray: record.uploadFile
                });
                me.getContent().add(panel);
            }
        });
    },
    getTextTitle: function () {
        if (!this.textTitle) {
            this.textTitle = new Ext.form.field.Text({
                emptyText: '标题',
                width: '100%'
            });
        }
        return this.textTitle;
    },
    getTextarea: function () {
        if (!this.textarea) {
            this.textarea = new Ext.form.field.TextArea({
                emptyText: '内容',
                height: 150,
                width: '100%'
            });
        }
        return this.textarea;
    },
    getUploadPanel: function () {
        var me = this;
        if (!me.uploadFile) {
            me.uploadFile = Ext.create('app.ux.UploadPanel');
        }
        return me.uploadFile;
    },
    getContent: function () {
        if (!this.content) {
            this.content = new Ext.panel.Panel({
                region: 'center',
                bodyStyle: 'padding:3px',
                autoScroll: true,
                border: true,
                items: []
            });
        }
        return this.content;
    },
    save: function () {
        var me = this;
        var title = me.getTextTitle().getValue();
        var content = me.getTextarea().getValue();
        if (title == "" || confirm == "") {
            Ext.Msg.alert('系统提示', '请输入标题和内容');
            return false;
        }
        var file = me.getUploadPanel().getValue();

        App.AjaxPost('/api/services/app/topic/CreateTopic', { ProjectId: me.projectId, Title: title, Content: content, UploadFile: file }, function () {
            me.textReset();
            me.initReplies(me);
        });
    }
})